<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\learn\article;

use app\api\controller\Controller;
use app\api\model\learn\ArticleCategory as BookModel;
use app\api\model\learn\article\Book as ArticleModel;
use app\api\model\learn\article\CategoryUser as CategoryUserModel;
use app\api\model\learn\article\ReviewUser as TitleReviewModel;
use app\api\model\learn\article\TitleUser as TitleUserModel;
use app\api\service\User as UserService;
use think\facade\Db;
use think\response\Json;

/**
 * 条文
 * Class Category
 * @package app\api\controller\article
 */
class Index extends Controller {

	//条文设置

	public function settingArticle(int $num, int $bookId) {
		$userId = UserService::getCurrentLoginUserId();

		Db::name('learn_article_category_user')->where('book_id', $bookId)->where('user_id', $userId)->update(['num' => $num]);

		return $this->renderSuccess();
	}

	//计算完成时间

	public function getComplateDate(int $num, int $totalNum) {
		$info['day'] = round($totalNum / $num);
		$time = time() + $info['day'] + $info['day'] * 86400;
		$info['date'] = Date('Y-m-d', (int) $time);

		return $this->renderSuccess(compact('info'));
	}

	public function defaultBook(): Json {
		$userId = UserService::getCurrentLoginUserId();
		$bookId = Db::name('user')->where('user_id', $userId)->value('article_id');

		$info = CategoryUserModel::detail(['user_id' => $userId, 'book_id' => $bookId], ['book']);
		if ($info) {
			$info['today_num'] = Db::name('learn_article_title_user')->where('user_id', $userId)
				->where('book_id', $bookId)->where('status', '>', 0)->where('date', Date('Y-m-d'))->count();
		}

		return $this->renderSuccess(compact('info'));
	}

	/**
	 * 文章分类列表
	 * @return Json
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function list(): Json {
		$model = new BookModel;
		$list = $model->getList();
		return $this->renderSuccess(compact('list'));
	}
	//我的条文列表
	public function todayList(int $type, int $bookId): Json {
		$model = new TitleUserModel;

		$list = $model->getList($type, $bookId);
		return $this->renderSuccess(compact('list'));
	}
	public function detail(int $articleId): Json {
		$detail = ArticleModel::getDetail($articleId);
		//背诵心得
		$detail['comment'] = Db::name('learn_article_comment')->where('article_id', $detail['article_id'])->count();
		//拆解
		$exList = explode('/', trim($detail['hollow_easy']));
		$list = [];
		foreach ($exList as $key => $value) {

			$list[] = $value;
			$list[] = 1;
		}
		array_pop($list);

		return $this->renderSuccess(compact('detail', 'list'));
	}

	//学习计划
	public function planList() {
		$userId = UserService::getCurrentLoginUserId();
		$model = new CategoryUserModel();

		$waitList = $model->getListWait($userId);
		$complateList = $model->getListComplate($userId);

		return $this->renderSuccess(compact('waitList', 'complateList'));
	}
	//用户条文详情
	public function userBook(int $bookId = 0) {
		$userId = UserService::getCurrentLoginUserId();
		$model = new CategoryUserModel();

		$info = $model->bookInfo($userId, $bookId);

		return $this->renderSuccess(compact('info'));
	}
	public function setDefault(int $bookId) {
		$userId = UserService::getCurrentLoginUserId();
		$model = BookModel::detail($bookId);

		$model->setDefault($userId);

		return $this->renderSuccess();
	}
	//用户学习，获取当前题目
	public function getTimu(int $bookId) {
		$userId = UserService::getCurrentLoginUserId();
		$model = new CategoryUserModel();

		$info = $model->getTimu($userId, $bookId);

		return $this->renderSuccess(compact('info'));
	}
	//用户学习，获取下一题
	public function getNextId(int $articleId, int $bookId, int $review = 0) {
		$userId = UserService::getCurrentLoginUserId();
		if ($review == 1) {
			$articleId = Db::name('learn_article_title_review')->where('book_id', $bookId)
				->where('user_id', $userId)->order('log_id asc')->value('article_id');
			$articleId = $articleId ? $articleId : 0;
		} else {
			$articleId = Db::name('learn_article_title_user')->where('book_id', $bookId)
				->where('user_id', $userId)->where('article_id', '>', $articleId)->value('article_id');
			$articleId = $articleId ? $articleId : 0;
		}

		return $this->renderSuccess(compact('articleId'));
	}
	//复习条文列表
	public function reviewList(int $bookId) {
	
		$model = new TitleUserModel();

		$list = $model->getReviewList($bookId);

		return $this->renderSuccess(compact('list'));
	}
	//提示次数
	public function review() {
		$userId = UserService::getCurrentLoginUserId();
		$model = TitleUserModel::detail(['user_id' => $userId, 'article_id' => $this->postForm()['articleId']]);

		$info = $model->review($model['review_num'] + 1, $this->postForm()['time']);

		return $this->renderSuccess();
	}
	//删除复习
	public function reviewDel() {

		$model = new TitleUserModel;

		$model->reviewDel($this->postForm()['logIds']);

		return $this->renderSuccess();
	}
	//我认识
	public function handRen() {
		$userId = UserService::getCurrentLoginUserId();
		$model = TitleUserModel::detail(['user_id' => $userId, 'article_id' => $this->postForm()['articleId']]);

		$info = $model->handren($this->postForm());
		if ($this->postForm()['is_review'] == 1) {

			$model2 = new TitleReviewModel;

			$model2->add($userId, $this->postForm());
		}

		return $this->renderSuccess();
	}
	//标志简单
	public function handEasy() {
		$userId = UserService::getCurrentLoginUserId();
		$model = TitleUserModel::detail(['user_id' => $userId, 'article_id' => $this->postForm()['articleId']]);
		if ($model['is_easy'] == 0) {
			$model->handEasy();
		}

		return $this->renderSuccess();
	}
}